package com.great.meal.search.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.great.meal.search.dao.SenderDao;
import com.great.meal.search.domain.Sender;
import com.great.meal.search.validator.SenderValidator;


@Controller
@RequestMapping("/sender.do")
public class SenderController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private  SenderDao senderDao;
	
	/**
	 *   判断送餐员名称是否已存在
	 * @param senderName
	 * @param response
	 * @return
	 */
	@RequestMapping(params= "method=isExist")
	public String isExist(@RequestParam("account") String account,HttpServletResponse response){
		PrintWriter out;
		try {
	
			
			out = response.getWriter();
			out.print("wwww".equals(account));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;		
	}
	/**
	 * 用户登录
	 * @param sender
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=loginSender")
	public String logonsender(HttpServletRequest request,HttpServletResponse response){
				
		return "sendInformation";
	}
	
	/**
	 * 注册送餐员
	 * @param sender
	 * @param result
	 * @param status
	 * @return
	 */
	@RequestMapping(params = "method=addSender")
	
	public String addsender(Sender sender, BindingResult result, SessionStatus status){
		new SenderValidator().validate(sender, result);
		if(result.hasErrors()){
			return "registerUser";
		} else{
			this.senderDao.saveOrUpdate(sender);
			status.setComplete();
			return "index";
		}
	}
	
	@RequestMapping(params = "method=getSender")
	public ModelMap getsender(@RequestParam("senderId") Long senderId){
		ModelMap map;
		try{
			map = new ModelMap(this.senderDao.getById(senderId));
			logger.info("load sender.do " + this.senderDao.getTClass());
			return map;
		} catch (Exception e){
			logger.info("load sender.do " + e + this.senderDao.getTClass());
			return null;
		}
	}
	/**
	 * 修改送餐员信息
	 *
	 * @param sender
	 * @return
	 */
	@RequestMapping(params = "method=editSender")
	public String editsender(@RequestParam("senderId") Long senderId,Sender sender){
		
		
		
		return null;
		
	}
	
	/**
	 * 删除送餐员
	 * @return
	 */
	@RequestMapping(params = "method=deletesender")
	public String deletesender(Sender sender){
		
		this.senderDao.delete(sender);
		
		return null;
		
	}
	
		
		

}
